﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Text;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using DevExpress.Web.ASPxUploadControl;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{

    /// <summary>
    /// 工作计划和总结
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class MonthPlanSummaryController : AdminBaseController
    {
        /// <summary>
        /// 私有的月工作BLL
        /// </summary>
        private BLL.MonthPlanSummary monthPlanSummaryBLL = new BLL.MonthPlanSummary();
        /// <summary>
        /// 私有的人员BLL
        /// </summary>
        private BLL.HREmpl emplBLL = new BLL.HREmpl();

        /// <summary>
        /// 系统参数逻辑类
        /// </summary>
        private BLL.SysParam sysParamBLL = new BLL.SysParam();
        /// <summary>
        /// 登录人员逻辑类
        /// </summary>
        private BLL.PrvUser prvUserBLL = new BLL.PrvUser();
        //
        // GET: /Admin/MonthPlan/
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// 
        [AuthorityActionFilter("m_MonthPlanSummary", Order = 1)]
        [LogPageActionFilter("工作计划和总结", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 上传党风廉政页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Upload() 
        {
            DataSet ds = monthPlanSummaryBLL.GetDept();
            string strDeptName = "";
            string strFile = "";
            StringBuilder deptNo = new StringBuilder();
            if (ds.Tables[0].Rows.Count > 0) 
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["FAttachOldFileName"].ToString() == "")
                    {
                        strFile += "暂时没有上传文件" + ",";

                    }
                    else
                    {
                        strFile += ds.Tables[0].Rows[i]["FAttachOldFileName"].ToString() + ",";

                    }
                    strDeptName += ds.Tables[0].Rows[i]["FDeptName"].ToString() + ",";
                    deptNo.Append(ds.Tables[0].Rows[i]["FDeptNo"].ToString() + ",");
                }
                strDeptName = strDeptName.Substring(0, strDeptName.LastIndexOf(","));
                strFile = strFile.Substring(0, strFile.LastIndexOf(","));
            }
            string[] arrDeptName = strDeptName.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["arrDeptName"] = arrDeptName;
            string[] arrstrFile = strFile.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["arrstrFile"] = arrstrFile;
            ViewData["deptNo"] = deptNo;


            Model.ICACDeptDoc icacDeptDocModel = new Model.ICACDeptDoc();
            BLL.ICACDocument icacDoucumentBll = new BLL.ICACDocument();
            Model.ICACDocument icacDoucumentModel = new Model.ICACDocument();
            DataSet dsicacDoucument = icacDoucumentBll.GetPath();
            ViewData["dw"] = "暂时没有上传文件";
            ViewData["ns"] = "暂时没有上传文件";
            StringBuilder dw = new StringBuilder();
            StringBuilder ns = new StringBuilder();
            if (dsicacDoucument.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsicacDoucument.Tables[0].Rows.Count; i++)
                {
                    if (dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString().Trim() == "单位廉效风险防控登记表")
                    {
                        ViewData["dw"] = dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString();
                        dw.Append(dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString());
                    }
                    else
                    {
                        ViewData["ns"] = dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString();
                        ns.Append(dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString());
                    }
                }
            }

            ViewData["dwtitle"] = dw;
            ViewData["nstitle"] = ns;

            ElingOA6_Admin_DSJCJ.Areas.Admin.Models.WeekPlanViewModel weekPlanViewModel =
              new Models.WeekPlanViewModel(icacDoucumentModel, icacDeptDocModel);

            return View("Upload", weekPlanViewModel);
        }

        /// <summary>
        /// 保存附件
        /// </summary>
        /// <param name="icacDoucumentModel">单位，内设机构的model对象</param>
        /// <param name="icacDeptDoc">个人的model的对象</param>
        /// <returns>视图</returns>
        public ActionResult SaveFile(Model.ICACDocument icacDoucumentModel, Model.ICACDeptDoc icacDeptDocModel)
        {
            BLL.ICACDocument icacDoucumentBll = new BLL.ICACDocument();
            DataSet dsicacDoucument = icacDoucumentBll.GetPath();
            ViewData["dw"] = "#";
            ViewData["ns"] = "#";
            if (dsicacDoucument.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsicacDoucument.Tables[0].Rows.Count; i++)
                {
                    if (dsicacDoucument.Tables[0].Rows[i]["FAttachDesc"].ToString() == "单位廉效风险防控登记表")
                    {
                        ViewData["dw"] = "~" + dsicacDoucument.Tables[0].Rows[i]["FAttachSvrFileName"].ToString();
                    }
                    else
                    {
                        ViewData["ns"] = "~" + dsicacDoucument.Tables[0].Rows[i]["FAttachSvrFileName"].ToString();
                    }
                }
            }


            BLL.ICACDeptDoc icacDeptDocBll = new BLL.ICACDeptDoc();
            DataSet dsicacDeptDoc = icacDeptDocBll.GetPath();
            string strPath = "";
            if (dsicacDeptDoc.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsicacDeptDoc.Tables[0].Rows.Count; i++)
                {
                    strPath += "~" + dsicacDeptDoc.Tables[0].Rows[i]["FAttachSvrFileName"].ToString() + ",";
                }
            }
            string[] arrPath = strPath.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            DataSet ds = monthPlanSummaryBLL.GetDept();
            string strDeptName = "";
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    strDeptName += ds.Tables[0].Rows[i]["FDeptName"].ToString() + ",";
                }
            }
            string[] arrDeptName = strDeptName.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["arrDeptName"] = arrDeptName;

            ActionResult actionResult = null;
            try
            {
                string dwtitle = Fetch.Post("hiddwtitle");

                ///附件上传
                UploadedFile[] uploadFiles = UploadControlExtension.GetUploadedFiles("uploaddw");
                if (uploadFiles.Length > 0 && null != uploadFiles)
                {
                    for (int i = 0; i < uploadFiles.Length; i++)
                    {
                        UploadedFile upload = uploadFiles[i];
                        if (upload.FileName != "")
                        {
                            if (upload != null)
                            {
                                string backname = upload.FileName.Substring(upload.FileName.LastIndexOf('.') + 1);

                                string entityNo = GlobalConstant.ENTITYNO;

                                string dest_dir = "Publish/jxtz/" + entityNo + "/upload/";

                                string svrFileName = Path.GetFileNameWithoutExtension(upload.FileName);//上传的文件名

                                string uploadoffiepath = Server.MapPath(Url.Content("~") + dest_dir);//上传文件的路径

                                if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                                {
                                    Directory.CreateDirectory(uploadoffiepath);
                                }

                                if (!icacDoucumentBll.Exists(dwtitle))
                                {
                                    icacDoucumentModel.FICACDocumentID = icacDoucumentModel.FICACDocumentID;
                                    icacDoucumentModel.FTitle = svrFileName;
                                    icacDoucumentModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachFileExt = backname;
                                    icacDoucumentModel.FAttachDesc = Fetch.Post("dwFAttachDesc");
                                    icacDoucumentModel.FInsDate = DateTime.Now;

                                    icacDoucumentBll = new BLL.ICACDocument();
                                    icacDoucumentBll.Add(icacDoucumentModel);
                                }
                                else
                                {
                                    icacDoucumentModel = icacDoucumentBll.GetModel(dwtitle);

                                    icacDoucumentModel.FICACDocumentID = icacDoucumentModel.FICACDocumentID;
                                    icacDoucumentModel.FTitle = svrFileName;
                                    icacDoucumentModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachFileExt = backname;
                                    icacDoucumentModel.FAttachDesc = Fetch.Post("dwFAttachDesc");
                                    icacDoucumentModel.FInsDate = DateTime.Now;
                                    ///
                                    List<string> imgPathList = GetPathListdw(dwtitle, icacDoucumentModel.FICACDocumentID);
                                    DeleteDoc(imgPathList);
                                    icacDoucumentBll.Update(icacDoucumentModel);
                                }

                                upload.SaveAs(uploadoffiepath + svrFileName + "." + backname);
                            }
                        }
                    }
                    actionResult = View("Upload", icacDoucumentModel);
                }

                string nstitle = Fetch.Post("hidnstitle");
                UploadedFile[] uploadFiles1 = UploadControlExtension.GetUploadedFiles("uploadnsjg");
                if (uploadFiles1.Length > 0 && null != uploadFiles1)
                {
                    for (int i = 0; i < uploadFiles1.Length; i++)
                    {
                        UploadedFile upload = uploadFiles1[i];
                        if (upload.FileName != "")
                        {
                            if (upload != null)
                            {
                                string backname = upload.FileName.Substring(upload.FileName.LastIndexOf('.') + 1);

                                string entityNo = GlobalConstant.ENTITYNO;

                                string dest_dir = "Publish/jxtz/" + entityNo + "/upload/";

                                string svrFileName = Path.GetFileNameWithoutExtension(upload.FileName);//上传的文件名

                                string uploadoffiepath = Server.MapPath(Url.Content("~") + dest_dir);//上传文件的路径

                                if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                                {
                                    Directory.CreateDirectory(uploadoffiepath);
                                }



                                if (!icacDoucumentBll.Exists(nstitle))
                                {

                                    icacDoucumentModel.FICACDocumentID = icacDoucumentModel.FICACDocumentID;
                                    icacDoucumentModel.FTitle = svrFileName;
                                    icacDoucumentModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachFileExt = backname;
                                    icacDoucumentModel.FAttachDesc = Fetch.Post("nsFAttachDesc");
                                    icacDoucumentModel.FInsDate = DateTime.Now;

                                    icacDoucumentBll = new BLL.ICACDocument();
                                    icacDoucumentBll.Add(icacDoucumentModel);
                                }
                                else
                                {

                                    icacDoucumentModel = icacDoucumentBll.GetModel(nstitle);
                                    icacDoucumentModel.FICACDocumentID = icacDoucumentModel.FICACDocumentID;
                                    icacDoucumentModel.FTitle = svrFileName;
                                    icacDoucumentModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                    icacDoucumentModel.FAttachFileExt = backname;
                                    icacDoucumentModel.FAttachDesc = Fetch.Post("nsFAttachDesc");
                                    icacDoucumentModel.FInsDate = DateTime.Now;

                                    ///
                                    List<string> imgPathList = GetPathListdw(nstitle, icacDoucumentModel.FICACDocumentID);
                                    DeleteDoc(imgPathList);

                                    icacDoucumentBll.Update(icacDoucumentModel);
                                }
                                upload.SaveAs(uploadoffiepath + svrFileName + "." + backname);
                            }
                        }
                    }
                    actionResult = View("Upload", icacDoucumentModel);
                }

                ///个人的原文件显示

                string deptNo = Fetch.Post("hiddeptNo");
                ViewData["deptNo"] = deptNo;

                string[] arrdeptNo = deptNo.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                ///个人
                for (int i = 0; i < arrDeptName.Length; i++)
                {
                    UploadedFile[] uploadFiles2 = UploadControlExtension.GetUploadedFiles("upload" + i);
                    if (uploadFiles2.Length > 0 && null != uploadFiles2)
                    {
                        for (int j = 0; j < uploadFiles2.Length; j++)
                        {
                            UploadedFile upload = uploadFiles2[j];
                            if (upload.FileName != "")
                            {
                                if (upload != null)
                                {
                                    string backname = upload.FileName.Substring(upload.FileName.LastIndexOf('.') + 1);

                                    string entityNo = GlobalConstant.ENTITYNO;

                                    string dest_dir = "Publish/jxtz/" + entityNo + "/upload/";

                                    string svrFileName = Path.GetFileNameWithoutExtension(upload.FileName);//上传的文件名

                                    string uploadoffiepath = Server.MapPath(Url.Content("~") + dest_dir);//上传文件的路径

                                    if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                                    {
                                        Directory.CreateDirectory(uploadoffiepath);
                                    }
                                    if (!icacDeptDocBll.Exists(entityNo, arrdeptNo[i]))
                                    {
                                        ///增加
                                        icacDeptDocModel.FICACDeptDocID = icacDeptDocModel.FICACDeptDocID;
                                        icacDeptDocModel.FEntityNo = GlobalConstant.ENTITYNO;
                                        icacDeptDocModel.FDeptNo = arrdeptNo[i];
                                        icacDeptDocModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                        icacDeptDocModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                        icacDeptDocModel.FAttachFileExt = backname;
                                        icacDeptDocModel.FAttachDesc = Fetch.Post("upload" + i);
                                        icacDeptDocModel.FInsDate = DateTime.Now;

                                        icacDeptDocBll = new BLL.ICACDeptDoc();
                                        icacDeptDocBll.Add(icacDeptDocModel);
                                    }
                                    else
                                    {

                                        List<string> imgPathList = GetPathList(arrdeptNo[i], entityNo);
                                        DeleteDoc(imgPathList);
                                        ///更新
                                        icacDeptDocModel = icacDeptDocBll.GetModel(entityNo, arrdeptNo[i]);
                                        icacDeptDocModel.FICACDeptDocID = icacDeptDocModel.FICACDeptDocID;
                                        icacDeptDocModel.FEntityNo = GlobalConstant.ENTITYNO;
                                        icacDeptDocModel.FDeptNo = arrdeptNo[i];
                                        icacDeptDocModel.FAttachOldFileName = Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                        icacDeptDocModel.FAttachSvrFileName = Url.Content("~") + dest_dir + Path.GetFileNameWithoutExtension(upload.FileName) + Path.GetExtension(upload.FileName).ToLower();
                                        icacDeptDocModel.FAttachFileExt = backname;
                                        icacDeptDocModel.FAttachDesc = Fetch.Post("upload" + i);
                                        icacDeptDocModel.FInsDate = DateTime.Now;

                                        icacDeptDocBll.Update(icacDeptDocModel);

                                    }

                                    upload.SaveAs(uploadoffiepath + svrFileName + "." + backname);
                                }
                            }

                        }
                    }
                    actionResult = View("Upload", icacDeptDocModel);
                }
                ViewData["message"] = "操作成功";
                ViewData["IsClose"] = "Y";

            }
            catch (Exception)
            {
                ViewData["message"] = "操作失败";
                ViewData["IsClose"] = "N";

            }
            string strFile= "";
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["FAttachOldFileName"].ToString() == "")
                    {
                        strFile += "暂时没有上传文件" + ",";
                    }
                    else
                    {
                        strFile += ds.Tables[0].Rows[i]["FAttachOldFileName"].ToString() + ",";
                    }
                }
            }
            string[] arrstrFile = strFile.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["arrstrFile"] = arrstrFile;
            return actionResult;
        }
        /// <summary>
        /// 删除保存在文件的图片
        /// </summary>
        /// <param name="strList">文件路径list</param>
        public void DeleteDoc(List<string> strList)
        {
            foreach (string strPath in strList)
            {
                GenerateUtil.DeleteFile(Url.Content("~") + strPath);
            }
        }
        /// <summary>
        /// 获取保存在硬盘中的文件路径
        /// </summary>
        /// <param name="title">主题</param>
        /// <param name="icacDocumentID">编号</param>
        /// <returns>list集合</returns>
        public List<string> GetPathListdw(string title, decimal icacDocumentID)
        {
            BLL.ICACDocument icacDoucumentBll = new BLL.ICACDocument();
            List<string> strList = icacDoucumentBll.GetSrvName(title, icacDocumentID);
            return strList;
        }

        /// <summary>
        /// 获取保存在硬盘中的文件路径
        /// </summary>
        /// <param name="dept">部门编号</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <returns>图片路径</returns>

        public List<string> GetPathList(string dept, string entityNo)
        {
            BLL.ICACDeptDoc icacDeptDocBll = new BLL.ICACDeptDoc();
            List<string> strList = icacDeptDocBll.GetSrvName(dept, entityNo);
            return strList;
        }

        /// <summary>
        /// 月工作计划和总结列表
        /// </summary>
        /// <returns></returns>
        public ActionResult MonthPlanSummaryList(string year = "") 
        {

            ViewData["CYear"] = DateTime.Now.Year;
            ViewData["CMonth"] = DateTime.Now.Month;

            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();

            string user_name = OperateSession.get(GlobalConstant.PARAM_USERNAME);
            ViewData["user_name"] = user_name;
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string entityNo = GlobalConstant.ENTITYNO;

            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            if (year == "")
            {                
                year = sysDT.Year.ToString();
            }
            string month = sysDT.Month.ToString();
            ViewData["year"] = year;
            ViewData["month"] = month;
            ViewData["emplNo"] = emplNo;
            //月工作计划和总结Table
            string monthPlanSummaryTable = GetMonthPlanSummary(year, month, emplNo, entityNo);
            ViewData["monthPlanSummaryTable"] = monthPlanSummaryTable;

            string strDept_left = GetLeftDept();
            ViewData["strDept_left"] = strDept_left;
            //单位和内设机构附件地址
            BLL.ICACDocument icacDoucumentBll = new BLL.ICACDocument();
            DataSet dsicacDoucument = icacDoucumentBll.GetPath();
            ViewData["dw"] = "#";
            ViewData["ns"] = "#";
            if (dsicacDoucument.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsicacDoucument.Tables[0].Rows.Count; i++)
                {
                    if (dsicacDoucument.Tables[0].Rows[i]["FTitle"].ToString().Trim() == "单位廉效风险防控登记表")
                    {
                        ViewData["dw"] = dsicacDoucument.Tables[0].Rows[i]["FAttachSvrFileName"].ToString();
                    }
                    else
                    {
                        ViewData["ns"] = dsicacDoucument.Tables[0].Rows[i]["FAttachSvrFileName"].ToString();
                    }
                }
            }
            //个人
            string strFileDept = GetLXFXTable();
            ViewData["strFileDept"] = strFileDept;
            
            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;

            bool check = Check(emplNo);
            if (check)
            {
                ViewData["check"] = "true";
            }
            else 
            {
                ViewData["check"] = "false";
            }
            return View(monthPlanSummaryModel);
        }
        /// <summary>
        /// 是否需要参与绩效考核
        /// </summary>
        /// <returns></returns>
        private bool Check(string emplNo) 
        {
            bool check = false;
            string allowCheck = monthPlanSummaryBLL.GetCheck(emplNo);
            if (allowCheck == "Y")
            {
                check = true;
            }
            return check;
        }

        /// <summary>
        /// 左边部门信息列表
        /// </summary>
        /// <returns></returns>
        public string GetLeftDept() 
        {
            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            string year = sysDT.Year.ToString();

            string strDpet = "";
            DataSet dsDept = monthPlanSummaryBLL.GetCheckDept();
            if (dsDept.Tables[0].Rows.Count > 0) 
            {
                for (int i = 0; i < dsDept.Tables[0].Rows.Count; i++) 
                {
                    DataSet dsEmpCheck = monthPlanSummaryBLL.GetChechEmp(dsDept.Tables[0].Rows[i]["FDeptNo"].ToString());
                    if (dsEmpCheck.Tables[0].Rows.Count > 0)
                    {
                        strDpet += "<div class=\"left_title\">" + dsDept.Tables[0].Rows[i]["FDeptName"].ToString() + "</div>";
                        strDpet += "<div class=\"left_con\">";
                        for (int j = 0; j < dsEmpCheck.Tables[0].Rows.Count; j++)
                        {
                            if (user != dsEmpCheck.Tables[0].Rows[j]["FEmplNo"].ToString())
                            {
                                strDpet += "<a href='" + Url.Action("LookMonthPlanSummary", "MonthPlanSummary", new { year = year, emplNo = dsEmpCheck.Tables[0].Rows[j]["FEmplNo"].ToString() }) + "' style= 'cursor:hand;' target=\"_blank\">";
                                strDpet += (dsEmpCheck.Tables[0].Rows[j]["FEmpName"].ToString());
                                strDpet += "</a>";
                            }
                            else 
                            {
                                strDpet += "<a href='" + Url.Action("MonthPlanSummaryList", "MonthPlanSummary", new { year = year, emplNo = dsEmpCheck.Tables[0].Rows[j]["FEmplNo"].ToString() }) + "' style= 'cursor:hand;' target=\"_blank\">";
                                strDpet += (dsEmpCheck.Tables[0].Rows[j]["FEmpName"].ToString());
                                strDpet += "</a>"; 
                            }
                        }
                        strDpet += "</div>";
                    }
                }
            }
            return strDpet;
        }

        /// <summary>
        /// 个人附件（部门）
        /// </summary>
        /// <returns></returns>
        public string GetLXFXTable() 
        {
            string strLXFXtable = "";
            BLL.ICACDeptDoc icacDeptDocBll = new BLL.ICACDeptDoc();
            DataSet dsicacDeptDoc = icacDeptDocBll.GetPath();
            if (dsicacDeptDoc.Tables[0].Rows.Count > 0)
            {
                for (int j = 0; j < dsicacDeptDoc.Tables[0].Rows.Count; j++)
                {
                    strLXFXtable += "<td height=\"50\" align=\"center\" width=\"40\"><a href='" + dsicacDeptDoc.Tables[0].Rows[j]["FAttachSvrFileName"].ToString() + "'>";
                    strLXFXtable += dsicacDeptDoc.Tables[0].Rows[j]["FAttachOldFileName"].ToString();
                    strLXFXtable += "</a></td>";
                    if ((j + 1) % 8 == 0)
                    {
                        strLXFXtable += "</tr>";
                        strLXFXtable += "<tr>";
                    }
                }
                if (dsicacDeptDoc.Tables[0].Rows.Count % 8 != 0)
                {
                    int hr = dsicacDeptDoc.Tables[0].Rows.Count % 8;
                    hr = 8 - hr;
                    for (int k = 0; k < hr; k++)
                    {
                        strLXFXtable += "<td width=\"40\">";
                        strLXFXtable += "&nbsp;</td>";
                    }
                }

            }
            return strLXFXtable;
        }

        /// <summary>
        /// 判断第一张表是否有权限
        /// </summary>
        /// <returns></returns>
        public string PermissionFirstTable() 
        {
            string str = "N";
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            DAL.PrvFunctList prvFuncListDal = new DAL.PrvFunctList();
            List<Model.PrvFunctList> prvFunctList = prvFuncListDal.GetPrvFunctList(emplNo);
            var query1 = prvFunctList.Where(s => s.FFunctNo == "m_AcDeptTimesList_new");
            if (query1.Count() > 0)
            {
                str = "Y";
            }
            return str;
        }
        /// <summary>
        /// 判断第二张表是否有权限
        /// </summary>
        /// <returns></returns>
        public string PermissionSecondTable() 
        {
            string str = "N";
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            DAL.PrvFunctList prvFuncListDal = new DAL.PrvFunctList();
            List<Model.PrvFunctList> prvFunctList = prvFuncListDal.GetPrvFunctList(emplNo);
            var query1 = prvFunctList.Where(s => s.FFunctNo == "m_NoMonthPlanSummaryList");
            if (query1.Count() > 0)
            {
                str = "Y";
            }
            return str;
        }

        /// <summary>
        /// 月工作计划和总结读取 自己页面
        /// </summary>
        /// <returns></returns>
        public string GetMonthPlanSummary(string year, string month, string emplNo, string entityNo) 
        {
            string strTable = "";
            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            //12个月
            for (int i = 1; i <= 12; i++) 
            {                
                if (i != decimal.Parse(month))
                {
                    strTable += "<div class=\"nl_div\" height=\"185\">";
                    strTable += "<table width=\"200\" height=\"185\" border=\"0\">";
                    strTable += "<tr><td height=\"45\" align=\"center\"><b><a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\" target=\"_blank\">" + i + "月</a></b></td></tr>";
                    strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">本月总结</td></tr>";
                    List<Model.MonthPlanSummary> monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + i + " AND FType='S' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                    strTable += "<tr><td height=\"40\">";
                    if (monthPlanSummaryList.Count > 0)
                    {
                        if (monthPlanSummaryList[0].FContent.Length <= 48)
                        {
                            strTable += monthPlanSummaryList[0].FContent;
                        }
                        else
                        {
                            strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                        }
                    }
                    else 
                    {
                        strTable += "";
                    }
                    strTable += "</td></tr>";
                    strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">下月计划</td></tr>";
                    monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + (i + 1) + " AND FType='P' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                    strTable += "<tr><td height=\"40\">";
                    if (monthPlanSummaryList.Count > 0)
                    {
                        if (monthPlanSummaryList[0].FContent.Length <= 48)
                        {
                            strTable += monthPlanSummaryList[0].FContent;
                        }
                        else
                        {
                            strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                        }
                    }
                    else
                    {
                        strTable += "";
                    }
                    strTable += "</td></tr></table></div>";
                }
                else 
                {
                    if (year == sysDT.Year.ToString())
                    {
                        strTable += "<div class=\"nl_div\" style=\"background:#c9dae2;\" height=\"185\">";
                        strTable += "<table width=\"200\" height=\"185\" border=\"0\">";
                        strTable += "<tr><td height=\"45\" align=\"center\"><b><a href=\"" + Url.Action("MonthPlanSummaryAdd", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\" target=\"_blank\">" + i + "月</a></b></td></tr>";
                        strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">本月总结</td></tr>";
                        List<Model.MonthPlanSummary> monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + i + " AND FType='S' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                        strTable += "<tr><td height=\"40\">";
                        if (monthPlanSummaryList.Count > 0)
                        {
                            if (monthPlanSummaryList[0].FContent.Length <= 48)
                            {
                                strTable += monthPlanSummaryList[0].FContent;
                            }
                            else
                            {
                                strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                            }
                        }
                        else
                        {
                            strTable += "";
                        }
                        strTable += "</td></tr>";
                        strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">下月计划</td></tr>";
                        monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + (i + 1) + " AND FType='P' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                        strTable += "<tr><td height=\"40\">";
                        if (monthPlanSummaryList.Count > 0)
                        {
                            if (monthPlanSummaryList[0].FContent.Length <= 48)
                            {
                                strTable += monthPlanSummaryList[0].FContent;
                            }
                            else
                            {
                                strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                            }
                        }
                        else
                        {
                            strTable += "";
                        }
                        strTable += "</td></tr></table></div>";
                    }
                    else 
                    {

                        strTable += "<div class=\"nl_div\" height=\"185\">";
                        strTable += "<table width=\"200\" height=\"185\" border=\"0\">";
                        strTable += "<tr><td height=\"45\" align=\"center\"><b><a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\" target=\"_blank\">" + i + "月</a></b></td></tr>";
                        strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">本月总结</td></tr>";
                        List<Model.MonthPlanSummary> monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + i + " AND FType='S' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                        strTable += "<tr><td height=\"40\">";
                        if (monthPlanSummaryList.Count > 0)
                        {
                            if (monthPlanSummaryList[0].FContent.Length <= 48)
                            {
                                strTable += monthPlanSummaryList[0].FContent;
                            }
                            else
                            {
                                strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                            }
                        }
                        else
                        {
                            strTable += "";
                        }
                        strTable += "</td></tr>";
                        strTable += "<tr><td class=\"font14 fontbold\" height=\"30\">下月计划</td></tr>";
                        monthPlanSummaryList = monthPlanSummaryBLL.GetModelList(" FYear=" + decimal.Parse(year) + " AND FMonth=" + (i+1) + " AND FType='P' AND FEmplNo='" + emplNo + "' AND FEntityNo='" + entityNo + "'");
                        strTable += "<tr><td height=\"40\">";
                        if (monthPlanSummaryList.Count > 0)
                        {
                            if (monthPlanSummaryList[0].FContent.Length <= 48)
                            {
                                strTable += monthPlanSummaryList[0].FContent;
                            }
                            else
                            {
                                strTable += monthPlanSummaryList[0].FContent.Substring(0, 47) + "...";
                            }
                        }
                        else
                        {
                            strTable += "";
                        }
                        strTable += "</td></tr></table></div>";
                    
                    }
                }
            }
            return strTable;
        }

        /// <summary>
        /// 工作计划和总结录入页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MonthPlanSummaryAdd(string year = "", string month = "", string emplNo = "") 
        {
            ViewData["CYear"] = DateTime.Now.Year;
            ViewData["CMonth"] = DateTime.Now.Month;

            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();

            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            if (year == "") 
            {
                year = sysDT.Year.ToString();
            }
            if (month == "") 
            {
                month = sysDT.Month.ToString();
            }
            if (emplNo == "") 
            {
                emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            }

            int monthInt = Int32.Parse(month);
            int yearInt = Int32.Parse(year);
            if (monthInt > 12) 
            {
                monthInt = 1;
                yearInt++;
            }

            if (monthInt < 1) 
            {
                monthInt = 12;
                yearInt--;
            }

            ViewData["year"] = yearInt.ToString();
            ViewData["month"] = monthInt.ToString();
            ViewData["nextmonth"] = (monthInt + 1).ToString();
            ViewData["emplNo"] = emplNo;
            string emplName = monthPlanSummaryBLL.GetEmpName(emplNo);
            ViewData["emplName"] = emplName;
            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;

            //下个月
            int nextMonth = 0;
            int nextYear = 0;
            ViewData["year"] = yearInt.ToString();
            ViewData["month"] = monthInt.ToString();
            if (monthInt + 1 > 12)
            {
                ViewData["nextmonth"] = "1";
                nextMonth = 1;
                nextYear = yearInt + 1;
            }
            else
            {
                ViewData["nextmonth"] = (monthInt + 1).ToString();
                nextMonth = monthInt + 1;
                nextYear = yearInt;
            }

            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            bool allowAdd = AllowAddtionInsPlan(user);
            if (allowAdd)
            {
                ViewData["allow"] = "true";
            }
            else 
            {
                ViewData["allow"] = "false";
            }

            //计划
            string type = "P";
            string entityNo = GlobalConstant.ENTITYNO;
            if (!monthPlanSummaryBLL.Exists(decimal.Parse(nextYear.ToString()), decimal.Parse((nextMonth).ToString()), emplNo, entityNo, type))
            {
                ViewData["typePlan"] = "ADD";
                ViewData["upPlan"] = true;
                ViewData["FContentP"] = "";
            }
            else
            {
                ViewData["typePlan"] = "EDIT";
                ViewData["upPlan"] = false;
                monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(decimal.Parse(nextYear.ToString()), decimal.Parse((nextMonth).ToString()), emplNo, entityNo, type);
                ViewData["FContentP"] = monthPlanSummaryModel.FContent;
            }

            //总结
            type = "S";
            if (!monthPlanSummaryBLL.Exists(decimal.Parse(yearInt.ToString()), decimal.Parse(monthInt.ToString()), emplNo, entityNo, type))
            {
                ViewData["typeSummary"] = "ADD";
                ViewData["upSummary"] = true;
                ViewData["FContentS"] = "";
            }
            else
            {
                ViewData["typeSummary"] = "EDIT";
                ViewData["upSummary"] = false;
                monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(decimal.Parse(yearInt.ToString()), decimal.Parse(monthInt.ToString()), emplNo, entityNo, type);
                ViewData["FContentS"] = monthPlanSummaryModel.FContent;
            }

            return View(monthPlanSummaryModel);
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <returns></returns> 
        [ValidateInput(false)]
        public ActionResult SaveMonthPlanSummary() 
        {
            ActionResult actionResult = null;
            string typePlan = Fetch.Post("hidtypePlan");
            string typeSummary = Fetch.Post("hidtypeSummary");
            string emplNo = Fetch.Post("hidemplNo");
            string year = Fetch.Post("hidyear");
            string month = Fetch.Post("hidmonth");
            string nextmonth = Fetch.Post("hidnextmonth");
            int count = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));

            int monthInt = Int32.Parse(month);
            int yearInt = Int32.Parse(year);
            if (monthInt > 12)
            {
                monthInt = 1;
                yearInt++;
            }

            if (monthInt < 1)
            {
                monthInt = 12;
                yearInt--;
            }
            //下个月
            int nextMonth = 0;
            int nextYear = 0;
            ViewData["year"] = yearInt.ToString();
            ViewData["month"] = monthInt.ToString();
            if (monthInt + 1 > 12)
            {
                ViewData["nextmonth"] = "1";
                nextMonth = 1;
                nextYear = yearInt + 1;
            }
            else
            {
                ViewData["nextmonth"] = (monthInt + 1).ToString();
                nextMonth = monthInt + 1;
                nextYear = yearInt;
            }
            int countNext = DateTime.DaysInMonth(nextYear, nextMonth);


            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);//补录的人员

            string deptNo = emplBLL.GetEmplFromViewByEmplNo(emplNo, GlobalConstant.ENTITYNO).Tables[0].Rows[0]["FDeptNo"].ToString();

            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();                     
            monthPlanSummaryModel.FEntityNo = GlobalConstant.ENTITYNO;
            monthPlanSummaryModel.FEmplNo = emplNo;
                      
            monthPlanSummaryModel.FDeptNo = deptNo;
            //工作计划的更新和添加
            try
            {
                //计划保存和修改
                bool upcheck = CheckBoxExtension.GetValue<bool>("upPlan");
                if (upcheck)
                {
                    if (typePlan == "ADD")
                    {
                        monthPlanSummaryModel.FBeginDate = DateTime.Parse(nextYear + "-" + nextmonth + "-01");
                        monthPlanSummaryModel.FEndDate = DateTime.Parse(nextYear + "-" + nextmonth + "-" + countNext);
                        monthPlanSummaryModel.FYear = decimal.Parse(nextYear.ToString());
                        monthPlanSummaryModel.FMonth = decimal.Parse(nextMonth.ToString());
                        monthPlanSummaryModel.FInsIP = GenerateUtil.GetClientIPv4Address();
                        monthPlanSummaryModel.FInsDate = DateTime.Now;
                        monthPlanSummaryModel.FType = "P";
                        monthPlanSummaryModel.FContent = checkStr(NoHTML(Fetch.Post("FContentP")));
                        if (monthPlanSummaryModel.FContent != "")
                        {
                            if (user == emplNo)
                            {
                                monthPlanSummaryBLL.Add(monthPlanSummaryModel);
                            }
                            else 
                            {
                                monthPlanSummaryModel.FAdditionalInsDate = DateTime.Now;
                                monthPlanSummaryModel.FAdditionalInsEmplNo = user;
                                monthPlanSummaryModel.FAdditionalInsIP = GenerateUtil.GetClientIPv4Address();
                                monthPlanSummaryBLL.Addition(monthPlanSummaryModel);
                            }
                        }
                    }
                    else
                    {
                        monthPlanSummaryModel.FMonth = decimal.Parse(nextmonth);
                        monthPlanSummaryModel.FUpdDate = DateTime.Now;
                        monthPlanSummaryModel.FType = "P";
                        monthPlanSummaryModel.FContent = checkStr(NoHTML(Fetch.Post("FContentP")));
                        if (monthPlanSummaryModel.FContent != "")
                        {
                            monthPlanSummaryBLL.Update(monthPlanSummaryModel);
                        }                       
                    }
                }
                //总结保存和修改
                upcheck = CheckBoxExtension.GetValue<bool>("upSummary");
                if (upcheck)
                {
                    if (typeSummary == "ADD")
                    {
                        monthPlanSummaryModel.FBeginDate = DateTime.Parse(year + "-" + month + "-01");
                        monthPlanSummaryModel.FEndDate = DateTime.Parse(year + "-" + month + "-" + count);
                        monthPlanSummaryModel.FYear = decimal.Parse(year); 
                        monthPlanSummaryModel.FMonth = decimal.Parse(month);
                        monthPlanSummaryModel.FInsIP = GenerateUtil.GetClientIPv4Address();
                        monthPlanSummaryModel.FInsDate = DateTime.Now;
                        monthPlanSummaryModel.FType = "S";
                        monthPlanSummaryModel.FContent = checkStr(NoHTML(Fetch.Post("FContentS")));
                        if (monthPlanSummaryModel.FContent != "")
                        {
                            if (user == emplNo)
                            {
                                monthPlanSummaryBLL.Add(monthPlanSummaryModel);
                            }
                            else
                            {
                                monthPlanSummaryModel.FAdditionalInsDate = DateTime.Now;
                                monthPlanSummaryModel.FAdditionalInsEmplNo = user;
                                monthPlanSummaryModel.FAdditionalInsIP = GenerateUtil.GetClientIPv4Address();
                                monthPlanSummaryBLL.Addition(monthPlanSummaryModel);
                            }
                        }                        
                    }
                    else
                    {
                        monthPlanSummaryModel.FMonth = decimal.Parse(month);
                        monthPlanSummaryModel.FUpdDate = DateTime.Now;
                        monthPlanSummaryModel.FType = "S";
                        monthPlanSummaryModel.FContent = checkStr(NoHTML(Fetch.Post("FContentS")));
                        if (monthPlanSummaryModel.FContent != "")
                        {
                            monthPlanSummaryBLL.Update(monthPlanSummaryModel);
                        }                       
                    }
                }


                ViewData["message"] = "操作成功";
                ViewData["IsClose"] = "Y";

                ViewData["RedirectURL"] = Url.Action("Index", "MonthPlanSummary");
                actionResult = View("SuccessMessagePage");
            }
            catch (Exception)
            {
                ViewData["message"] = "操作失败";
                ViewData["IsClose"] = "N";
            }
            return actionResult;
        }
        /// <summary>
        /// 替换Html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public string checkStr(string html)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex(@"<textarea[^>]+>).*?(<\/textarea>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            //html = regex10.Replace(html, "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            //html = html.Replace("###!!", "<br />");
            return html;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        private string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            //Htmlstring = Regex.Replace(Htmlstring, @"<br />", "###!!", RegexOptions.IgnoreCase);
            //Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "\r\n", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"\<textarea[^\>]+\<textarea\>", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "\r\n");

            return Htmlstring;
        }
        /// <summary>
        /// 查看页面
        /// </summary>
        /// <returns></returns>
        public ActionResult LookMonthPlanSummary(string year = "", string emplNo = "") 
        {
            ViewData["CYear"] = DateTime.Now.Year;
            ViewData["CMonth"] = DateTime.Now.Month;

            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());

            ViewData["user"] = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string strDept_left = GetLeftDept();
            ViewData["strDept_left"] = strDept_left;
            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;

            if (year == "")
            {                
                year = sysDT.Year.ToString();
            }
            string month = sysDT.Month.ToString();
            ViewData["year"] = year;
            ViewData["month"] = month;
            ViewData["emplNo"] = emplNo;
            string emplName = monthPlanSummaryBLL.GetEmpName(emplNo);
            ViewData["emplName"] = emplName;
            string monthPlanSummaryLookStr = GetMonthPlanSummary(year, month, emplNo);
            ViewData["monthPlanSummaryLookStr"] = monthPlanSummaryLookStr;

            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            bool check = Check(user);
            if (check)
            {
                ViewData["check"] = "true";
            }
            else
            {
                ViewData["check"] = "false";
            }

            return View();
        }
        /// <summary>
        /// 查看的页面
        /// </summary>
        /// <returns></returns>
        public string GetMonthPlanSummary(string year, string month, string emplNo) 
        {
            string strTable = "";
            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());

            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            bool allowAdd = AllowAddtionInsPlan(user);
            for (int i = 1; i <= 12; i++)
            {
                if (allowAdd)
                {
                    if (Int32.Parse(year) <= sysDT.Year)
                    {
                        if (i < decimal.Parse(month))
                        {
                            strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("MonthPlanSummaryAdd", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                        }
                        else if (i == decimal.Parse(month))
                        {
                            if (Int32.Parse(year) == sysDT.Year)
                            {
                                strTable += "<div class=\"grnl_div fong100\" style=\"background:#c9dae2;\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                            }
                            else if (Int32.Parse(year) < sysDT.Year)
                            {
                                strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("MonthPlanSummaryAdd", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                            }
                        }
                        else
                        {
                            if (Int32.Parse(year) == sysDT.Year)
                            {
                                strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                            }
                            else if (Int32.Parse(year) < sysDT.Year)
                            {
                                strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("MonthPlanSummaryAdd", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                            }
                        }
                    }
                    else 
                    {
                        strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                    }
                }
                else
                {
                    if (i != decimal.Parse(month))
                    {
                        strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                    }
                    else
                    {
                        if (Int32.Parse(year) == sysDT.Year)
                        {
                            strTable += "<div class=\"grnl_div fong100\" style=\"background:#c9dae2;\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                        }
                        else if (Int32.Parse(year) < sysDT.Year)
                        {
                            strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                        }
                        else
                        {
                            strTable += "<div class=\"grnl_div fong100\"><a target=\"_blank\" href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = i, emplNo = emplNo }) + "\">" + i + "月</a></div>";
                        }
                    }
                }
            }
            return strTable;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult DetailLookMonthPlanSummary(string year = "", string month = "", string emplNo = "") 
        {
            ViewData["CYear"] = DateTime.Now.Year;
            ViewData["CMonth"] = DateTime.Now.Month;

            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();
            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            string strDept_left = GetLeftDept();
            ViewData["strDept_left"] = strDept_left;
            if (year == "")
            {
                year = sysDT.Year.ToString();
            }
            if (month == "")
            {
                month = sysDT.Month.ToString();
            }
            int monthInt = Int32.Parse(month);
            int yearInt = Int32.Parse(year);
            if (monthInt > 12)
            {
                monthInt = 1;
                yearInt++;
            }

            if (monthInt < 1)
            {
                monthInt = 12;
                yearInt--;
            }
            //下个月
            int nextMonth = 0;
            int nextYear = 0;
            ViewData["year"] = yearInt.ToString();
            ViewData["month"] = monthInt.ToString();
            if (monthInt + 1 > 12)
            {
                ViewData["nextmonth"] = "1";
                nextMonth = 1;
                nextYear = yearInt + 1;
            }
            else
            {
                ViewData["nextmonth"] = (monthInt + 1).ToString();
                nextMonth = monthInt + 1;
                nextYear = yearInt;
            }
            ViewData["emplNo"] = emplNo;
            string emplName = monthPlanSummaryBLL.GetEmpName(emplNo);
            ViewData["emplName"] = emplName;
            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;

            //计划
            string type = "P";
            string entityNo = GlobalConstant.ENTITYNO;
            if (!monthPlanSummaryBLL.Exists(decimal.Parse(nextYear.ToString()), decimal.Parse(nextMonth.ToString()), emplNo, entityNo, type))
            {
                ViewData["FContentP"] = "未录入";
            }
            else
            {
                monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(decimal.Parse(nextYear.ToString()), decimal.Parse(nextMonth.ToString()), emplNo, entityNo, type);
                ViewData["FContentP"] = monthPlanSummaryModel.FContent;
            }

            //总结
            type = "S";
            if (!monthPlanSummaryBLL.Exists(decimal.Parse(yearInt.ToString()), decimal.Parse(monthInt.ToString()), emplNo, entityNo, type))
            {
                ViewData["FContentS"] = "未录入";
            }
            else
            {
                monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(decimal.Parse(yearInt.ToString()), decimal.Parse(monthInt.ToString()), emplNo, entityNo, type);
                ViewData["FContentS"] = monthPlanSummaryModel.FContent;
            }
            //是否需要参加绩效考核
            string user = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            bool check = Check(user);
            if (check)
            {
                ViewData["check"] = "true";
            }
            else
            {
                ViewData["check"] = "false";
            }
            ///是否有补录权限          
            bool allowAdd = AllowAddtionInsPlan(user);
            ViewData["allow"] = "true";
            if (allowAdd == false) 
            {
                ViewData["allow"] = "false";
            }
            return View(monthPlanSummaryModel);
        }

        /// <summary>
        /// 判断是否有权限补录
        /// </summary>
        /// <returns></returns>
        private bool AllowAddtionInsPlan(string emplNo) 
        {
            bool allowAdd = false;
            string allowAddStr = monthPlanSummaryBLL.GetAllowAddtionInsPlan(emplNo);
            if (allowAddStr == "Y") 
            {
                allowAdd = true;
            }
            return allowAdd;
        }
        /// <summary>
        /// 工作计划和总结未及时上报清单
        /// </summary>
        /// <returns></returns>
        public ActionResult NoMonthPlanSummaryList(string year = "", string month = "") 
        {
            ViewData["CYear"] = DateTime.Now.Year;
            ViewData["CMonth"] = DateTime.Now.Month;

            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            if (year == "")
            {
                year = sysDT.Year.ToString();
            }
            if (month == "")
            {
                month = sysDT.Month.ToString();
            }
            int monthInt = Int32.Parse(month);
            int yearInt = Int32.Parse(year);
            if (monthInt > 12)
            {
                monthInt = 1;
                yearInt++;
            }

            if (monthInt < 1)
            {
                monthInt = 12;
                yearInt--;
            }
            ViewData["year"] = yearInt.ToString();
            ViewData["month"] = monthInt.ToString();
            ViewData["emplNo"] = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;

            string loigID = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            if (strSecond != "Y")
            {
                Response.Write("<script>alert('对不起，您没有权限浏览该页面');window.location.href='" + Url.Action("MonthPlanSummaryList", "MonthPlanSummary", new { emplNo = loigID }) + "';</script>");
                Response.Flush();
                Response.End();
            }

            string strTableP = GetNoPlanListTable(yearInt, monthInt);
            string[] strArrayP = strTableP.Split(new string[] { "#$%" }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["strTableP"] = strArrayP[0];
            ViewData["countP"] = int.Parse(strArrayP[1]);

            string strTanleS = GetNoSummaryListTable(yearInt, monthInt);
            string[] strArrayS = strTanleS.Split(new string[] { "#$%" }, StringSplitOptions.RemoveEmptyEntries);
            ViewData["strTableS"] = strArrayS[0];
            ViewData["countS"] = int.Parse(strArrayS[1]);

            return View();
        }
        /// <summary>
        /// 未按时上报和未上报的Table 计划
        /// </summary>
        /// <returns></returns>
        private string GetNoPlanListTable(int year, int month) 
        {
            string str = "";
            string entityNo = GlobalConstant.ENTITYNO;
            string loginId = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);

            string leaderReportingDay = sysParamBLL.GetParamValue("OA_PlanReportDay_Leader", entityNo);
            string commonReportingDay = sysParamBLL.GetParamValue("OA_PlanReportDay_Common", entityNo);

            int counts = DateTime.DaysInMonth(year, month);

            string startDate = year + "-" + month + "-01";
            string endDate = year + "-" + month + "-" + counts;

            //需要绩效考核的人
            DataSet dtCheck = monthPlanSummaryBLL.GetListByEntityNo(entityNo, startDate, endDate);
            DataSet dtAllDaily = dtCheck.Clone();
            dtAllDaily.Tables.Add(new DataTable());
            //获取当前登录人是否有查看全局的绩效台账的权限
            Model.PrvUser prvUserModel = prvUserBLL.GetModel(loginId, entityNo);
            string isLookAllDaily = "N";
            string deptNos = "";
            if (prvUserModel != null)
            {
                isLookAllDaily = prvUserModel.FIsLookAllDaily;
                if (prvUserModel.FIsLookAllDaily == "N")
                {
                    //获取用户的所在部门，包括多岗位
                    DataSet dsEmplDept = emplBLL.GetEmplFromViewByEmplNo(emplNo, entityNo);

                    if (dsEmplDept.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = dsEmplDept.Tables[0];
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            deptNos += "'" + dt.Rows[i]["FDeptNo"].ToString() + "',";

                        }
                        deptNos = deptNos.Substring(0, deptNos.LastIndexOf(','));
                    }
                }

            }
            if (deptNos != "")
            {
                DataRow[] drs = dtCheck.Tables[0].Select("FDeptNo IN (" + deptNos + ")");

                foreach (DataRow dr in drs)
                {
                    dtAllDaily.Tables[0].Rows.Add(dr.ItemArray);
                }
            }
            else
            {
                if (isLookAllDaily == "Y")
                {
                    dtAllDaily = dtCheck;
                }
            }
            int count = 0;
            int max = 8;

            int urlMonth = month;
            int urlYear = year;
            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();
            if (DateTime.Now > DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + "26"))
            {
                if (dtAllDaily.Tables[0].Rows.Count > 0)
                {
                    string deptName = "";
                    int j = 0;
                    int sum = 0;
                    foreach (DataRow dr in dtAllDaily.Tables[0].Rows)
                    {
                        if (month >= 12)
                        {
                            month = 0;
                            year++;
                        }

                        monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(year, month + 1, dr["FEmplNo"].ToString(), entityNo, "P");
                        
                        //if (hrEmplDS.Tables[0].Rows[0]["FLeaveDate"].ToString()!="" &&)
                        //if (DateTime.Parse(hrEmplDS.Tables[0].Rows[0]["FLeaveDate"].ToString()) <= monthPlanSummaryModel.FEndDate || DateTime.Parse(hrEmplDS.Tables[0].Rows[0]["FWorkDate"].ToString()) <= monthPlanSummaryModel.FBeginDate)
                        //{
                            if (monthPlanSummaryModel == null)
                            {
                                if (deptName != dr["deptName"].ToString())
                                {
                                    deptName = dr["deptName"].ToString();

                                    if (j > 0)
                                    {
                                        str += "</table></td></tr>";
                                    }
                                    str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                    str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                    str += dr["FEmpName"].ToString();
                                    str += "</a>";

                                    j++;
                                    sum = 0;
                                    sum++;
                                    count++;
                                }
                                else
                                {
                                    if (sum > max)
                                    {
                                        str += "</td></tr><tr><td>";
                                        str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                        str += dr["FEmpName"].ToString();
                                        str += "</a>";

                                        sum = 0;
                                    }
                                    else
                                    {
                                        str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                        str += dr["FEmpName"].ToString();
                                        str += "</a>";
                                    }
                                    sum++;
                                    count++;
                                }
                            }
                            else
                            {
                                bool istrue = emplBLL.IsMainOrSub(dr["FEmplNo"].ToString());
                                //这条是补录的直接算按时
                                if (monthPlanSummaryModel.FAdditionalInsDate.ToString() == "")
                                {
                                    if (istrue)
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(urlYear.ToString() + "-" + urlMonth.ToString() + "-" + leaderReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();

                                                if (j > 0)
                                                {
                                                    str += "</table></td></tr>";
                                                }
                                                str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                                str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                                str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                str += dr["FEmpName"].ToString();
                                                str += "</a>";

                                                j++;
                                                sum = 0;
                                                sum++;
                                                count++;
                                            }
                                            else
                                            {
                                                if (sum > max)
                                                {
                                                    str += "</td></tr><tr><td>";
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";

                                                    sum = 0;
                                                }
                                                else
                                                {
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";
                                                }
                                                sum++;
                                                count++;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(urlYear.ToString() + "-" + urlMonth.ToString() + "-" + commonReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();

                                                if (j > 0)
                                                {
                                                    str += "</table></td></tr>";
                                                }
                                                str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                                str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                                str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                str += dr["FEmpName"].ToString();
                                                str += "</a>";

                                                j++;
                                                sum = 0;
                                                sum++;
                                                count++;
                                            }
                                            else
                                            {
                                                if (sum > max)
                                                {
                                                    str += "</td></tr><tr><td>";
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";

                                                    sum = 0;
                                                }
                                                else
                                                {
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = urlYear, month = urlMonth, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";
                                                }
                                                sum++;
                                                count++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        str += "</td></tr></table></td></tr>";
                    //}
                }
                else 
                {
                    str += "本月计划没有未按时上报的人员";
                }
            }
            else 
            {
                str += "本月计划没有未按时上报的人员";
            }
            return str + "#$%" + count.ToString();
        }
        /// <summary>
        /// 未按时上报和未上报的Table 总结
        /// </summary>
        /// <returns></returns>
        private string GetNoSummaryListTable(int year, int month) 
        {
            string str = "";
            string entityNo = GlobalConstant.ENTITYNO;
            string loginId = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);

            string leaderReportingDay = sysParamBLL.GetParamValue("OA_SummaryReportDay_Leader", entityNo);
            string commonReportingDay = sysParamBLL.GetParamValue("OA_SummaryReportDay_Common", entityNo);

            int counts = DateTime.DaysInMonth(year, month);

            string startDate = year + "-" + month + "-01";
            string endDate = year + "-" + month + "-" + counts;

            //需要绩效考核的人
            DataSet dtCheck = monthPlanSummaryBLL.GetListByEntityNo(entityNo, startDate, endDate);
            DataSet dtAllDaily = dtCheck.Clone();
            dtAllDaily.Tables.Add(new DataTable());
            //获取当前登录人是否有查看全局的绩效台账的权限
            Model.PrvUser prvUserModel = prvUserBLL.GetModel(loginId, entityNo);
            string isLookAllDaily = "N";
            string deptNos = "";
            if (prvUserModel != null)
            {
                isLookAllDaily = prvUserModel.FIsLookAllDaily;
                if (prvUserModel.FIsLookAllDaily == "N")
                {
                    //获取用户的所在部门，包括多岗位
                    DataSet dsEmplDept = emplBLL.GetEmplFromViewByEmplNo(emplNo, entityNo);

                    if (dsEmplDept.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = dsEmplDept.Tables[0];
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            deptNos += "'" + dt.Rows[i]["FDeptNo"].ToString() + "',";

                        }
                        deptNos = deptNos.Substring(0, deptNos.LastIndexOf(','));
                    }
                }

            }
            if (deptNos != "")
            {
                DataRow[] drs = dtCheck.Tables[0].Select("FDeptNo IN (" + deptNos + ")");

                foreach (DataRow dr in drs)
                {
                    dtAllDaily.Tables[0].Rows.Add(dr.ItemArray);
                }
            }
            else
            {
                if (isLookAllDaily == "Y")
                {
                    dtAllDaily = dtCheck;
                }
            }
            int count = 0;
            int max = 8;
            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();
            if (DateTime.Now > DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + "26"))
            {
                if (dtAllDaily.Tables[0].Rows.Count > 0)
                {
                    string deptName = "";
                    int j = 0;
                    int sum = 0;
                    foreach (DataRow dr in dtAllDaily.Tables[0].Rows)
                    {
                        
                        //DataSet hrEmplDS = monthPlanSummaryBLL.GetModelIsWork(dr["FEmplNo"].ToString(), entityNo);
                        monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(year, month, dr["FEmplNo"].ToString(), entityNo, "S");
                        //if (DateTime.Parse(hrEmplDS.Tables[0].Rows[0]["FLeaveDate"].ToString()) <= monthPlanSummaryModel.FEndDate || DateTime.Parse(hrEmplDS.Tables[0].Rows[0]["FWorkDate"].ToString()) <= monthPlanSummaryModel.FBeginDate)
                        //{
                            if (monthPlanSummaryModel == null)
                            {
                                if (deptName != dr["deptName"].ToString())
                                {
                                    deptName = dr["deptName"].ToString();

                                    if (j > 0)
                                    {
                                        str += "</table></td></tr>";
                                    }
                                    str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                    str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                    str += dr["FEmpName"].ToString();
                                    str += "</a>";

                                    j++;
                                    sum = 0;
                                    sum++;
                                    count++;
                                }
                                else
                                {
                                    if (sum > max)
                                    {
                                        str += "</td></tr><tr><td>";
                                        str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                        str += dr["FEmpName"].ToString();
                                        str += "</a>";

                                        sum = 0;
                                    }
                                    else
                                    {
                                        str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                        str += dr["FEmpName"].ToString();
                                        str += "</a>";
                                    }
                                    sum++;
                                    count++;
                                }
                            }
                            else
                            {
                                bool istrue = emplBLL.IsMainOrSub(dr["FEmplNo"].ToString());
                                if (monthPlanSummaryModel.FAdditionalInsDate.ToString() == "")
                                {
                                    if (istrue)
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + leaderReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();

                                                if (j > 0)
                                                {
                                                    str += "</table></td></tr>";
                                                }
                                                str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                                str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                                str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                str += dr["FEmpName"].ToString();
                                                str += "</a>";

                                                j++;
                                                sum = 0;
                                                sum++;
                                                count++;
                                            }
                                            else
                                            {
                                                if (sum > max)
                                                {
                                                    str += "</td></tr><tr><td>";
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";

                                                    sum = 0;
                                                }
                                                else
                                                {
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";
                                                }
                                                sum++;
                                                count++;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + commonReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();

                                                if (j > 0)
                                                {
                                                    str += "</table></td></tr>";
                                                }
                                                str += "<tr><td align=\"center\" width=\"100\"><strong>" + deptName + ":</strong></td>";
                                                str += "<td><table width=\"100%\"  class=\"wsb_table4\"><tr><td>";
                                                str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                str += dr["FEmpName"].ToString();
                                                str += "</a>";

                                                j++;
                                                sum = 0;
                                                sum++;
                                                count++;
                                            }
                                            else
                                            {
                                                if (sum > max)
                                                {
                                                    str += "</td></tr><tr><td>";
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";

                                                    sum = 0;
                                                }
                                                else
                                                {
                                                    str += "<a href=\"" + Url.Action("DetailLookMonthPlanSummary", "MonthPlanSummary", new { year = year, month = month, emplNo = dr["FEmplNo"].ToString() }) + "\" target=\"_blank\">";
                                                    str += dr["FEmpName"].ToString();
                                                    str += "</a>";
                                                }
                                                sum++;
                                                count++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        str += "</td></tr></table></td></tr>";

                    //}

                }
                else
                {
                    str += "本月总结没有未按时上报的人员";
                }
            }
            else
            {
                str += "本月总结没有未按时上报的人员";
            }
            return str + "#$%" + count.ToString();
        }
        /// <summary>
        /// 科室应扣次数
        /// </summary>
        /// <returns></returns>
        public ActionResult AcDeptTimesList() 
        {
            DataSet dsSysDate = monthPlanSummaryBLL.GetSysDate();
            DateTime sysDT = DateTime.Parse(dsSysDate.Tables[0].Rows[0]["sysDate"].ToString());
            int year = int.Parse(sysDT.Year.ToString());

            string strTable = GetAcDeptTimesTable(year);
            ViewData["strTable"] = strTable;

            //判断两张表的权限
            string strFirst = PermissionFirstTable();
            string strSecond = PermissionSecondTable();
            ViewData["strFirst"] = strFirst;
            ViewData["strSecond"] = strSecond;
            ViewData["emplNo"] = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            ViewData["year"] = year.ToString();
            string loigID=  OperateSession.get(GlobalConstant.PARAM_LOGINID);
            if (strFirst != "Y")
            {
                Response.Write("<script>alert('对不起，您没有权限浏览该页面');window.location.href='" + Url.Action("MonthPlanSummaryList", "MonthPlanSummary", new { emplNo = loigID }) + "';</script>");
                Response.Flush();
                Response.End();
            }

            return View();
        }
        /// <summary>
        /// 科室应扣次数table表
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private string GetAcDeptTimesTable(int year) 
        {
            string entityNo = GlobalConstant.ENTITYNO;            
            string str = "";
            string leaderReportingDay = sysParamBLL.GetParamValue("OA_PlanReportDay_Leader", entityNo);
            string commonReportingDay = sysParamBLL.GetParamValue("OA_PlanReportDay_Common", entityNo);           
            str += "<tr class=\"font14 fontbold\">";
            str += "<td width=\"86\">年</td>";
            str += "<td width=\"67\">月</td>";
            str += "<td width=\"130\">科室</td>";
            str += "<td width=\"155\">本月总结未按时上报人数</td>";
            str += "<td width=\"142\">下月计划未按时上报人数</td>";            
            str += "<td width=\"101\">应扣次数</td>";
            str += "</tr>";

            string deptName = "";
            int sum = 0;
            int sum1 = 0;
            List<int> intList = new List<int>();
            List<int> intListS = new List<int>();

            int urlI = 0;
            int urlJ = 0;
            Model.MonthPlanSummary monthPlanSummaryModel = new Model.MonthPlanSummary();
            //计划
            for (int i = 2014; i <= DateTime.Now.Year; i++) 
            {
                sum = 0;
                for (int j = 1; j <= 12; j++) 
                {
                    sum = 0;

                    urlI = i;
                    urlJ = j;
                    if (j < DateTime.Now.Month && i <= DateTime.Now.Year)
                    {
                        int counts = DateTime.DaysInMonth(year, j);
                        string startDate = year + "-" + j + "-01";
                        string endDate = year + "-" + j + "-" + counts;
                        DataSet dtCheck = monthPlanSummaryBLL.GetListByEntityNo(entityNo, startDate, endDate);

                        foreach (DataRow dr in dtCheck.Tables[0].Rows)
                        {
                            //计划
                            if (j >= 12)
                            {
                                j = 0;
                                i++;
                            }
                            monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(i, j + 1, dr["FEmplNo"].ToString(), entityNo, "P");
                            if (monthPlanSummaryModel == null)
                            {
                                if (deptName != dr["deptName"].ToString())
                                {
                                    deptName = dr["deptName"].ToString();
                                    if (sum > 0)
                                    {
                                        intList.Add(sum);
                                        sum = 0;
                                    }
                                }
                                sum++;
                            }
                            else
                            {
                                bool istrue = emplBLL.IsMainOrSub(dr["FEmplNo"].ToString());
                                 //这条是补录的直接算按时
                                if (monthPlanSummaryModel.FAdditionalInsDate.ToString() == "")
                                {
                                    if (istrue)
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(urlI.ToString() + "-" + urlJ.ToString() + "-" + leaderReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();
                                                if (sum > 0)
                                                {
                                                    intList.Add(sum);
                                                    sum = 0;
                                                }
                                            }
                                            sum++;
                                        }
                                    }
                                    else
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(urlI.ToString() + "-" + urlJ.ToString() + "-" + commonReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();
                                                if (sum > 0)
                                                {
                                                    intList.Add(sum);
                                                    sum = 0;
                                                }
                                            }
                                            sum++;
                                        }
                                    }
                                }
                            }                          
                        }
                        intList.Add(sum);                      
                    }
                }
            }
            //总结
            deptName = "";
            for (int i = 2014; i <= DateTime.Now.Year; i++)
            {
                sum1 = 0;
                for (int j = 1; j <= 12; j++)
                {
                    sum1 = 0;
                    if (j < DateTime.Now.Month && i <= DateTime.Now.Year)
                    {
                        int counts = DateTime.DaysInMonth(year, j);
                        string startDate = year + "-" + j + "-01";
                        string endDate = year + "-" + j + "-" + counts;
                        DataSet dtCheck = monthPlanSummaryBLL.GetListByEntityNo(entityNo, startDate, endDate);
                        foreach (DataRow dr in dtCheck.Tables[0].Rows)
                        {                           
                            //总结
                            monthPlanSummaryModel = monthPlanSummaryBLL.GetModel(i, j, dr["FEmplNo"].ToString(), entityNo, "S");
                            
                            if (monthPlanSummaryModel == null)
                            {
                                if (deptName != dr["deptName"].ToString())
                                {
                                    deptName = dr["deptName"].ToString();
                                    if (sum1 > 0)
                                    {
                                        intListS.Add(sum1);
                                        sum1 = 0;
                                    }
                                }
                                sum1++;
                            }
                            else
                            {
                                bool istrue = emplBLL.IsMainOrSub(dr["FEmplNo"].ToString());
                                if (monthPlanSummaryModel.FAdditionalInsDate.ToString() == "")
                                {
                                    if (istrue)
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(i.ToString() + "-" + j.ToString() + "-" + leaderReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();
                                                if (sum1 > 0)
                                                {
                                                    intListS.Add(sum1);
                                                    sum1 = 0;
                                                }
                                            }
                                            sum1++;
                                        }
                                    }
                                    else
                                    {
                                        if (monthPlanSummaryModel.FInsDate.Date > DateTime.Parse(i.ToString() + "-" + j.ToString() + "-" + commonReportingDay).Date)
                                        {
                                            if (deptName != dr["deptName"].ToString())
                                            {
                                                deptName = dr["deptName"].ToString();
                                                if (sum1 > 0)
                                                {
                                                    intListS.Add(sum1);
                                                    sum1 = 0;
                                                }
                                            }
                                            sum1++;
                                        }
                                    }
                                }
                            }
                        }
                       intListS.Add(sum1);
                    }
                }
            }
            deptName = "";
            int a = 0;
            for (int i = 2014; i <= DateTime.Now.Year; i++)
            {
                for (int j = 1; j <= 12; j++)
                {
                    if (j < DateTime.Now.Month && i <= DateTime.Now.Year)
                    {
                        int counts = DateTime.DaysInMonth(year, j);
                        string startDate = year + "-" + j + "-01";
                        string endDate = year + "-" + j + "-" + counts;
                        DataSet dtCheck = monthPlanSummaryBLL.GetListByEntityNo(entityNo, startDate, endDate);
                        for (int k = 0; k < dtCheck.Tables[0].Rows.Count; k++)
                        {
                            if (deptName != dtCheck.Tables[0].Rows[k]["deptName"].ToString())
                            {
                                deptName = dtCheck.Tables[0].Rows[k]["deptName"].ToString();
                                int P = intList[a];
                                int S = intListS[a];
                                int PS = P + S;
                                if (PS != 0)
                                {
                                    str += "<tr class=\"font14 fontbold\">";
                                    str += "<td width=\"86\">" + i + "</td>";
                                    str += "<td width=\"67\">" + j + "</td>";
                                    str += "<td width=\"130\">" + dtCheck.Tables[0].Rows[k]["deptName"].ToString() + "</td>";                                   
                                    str += "<td width=\"155\">" + S + "</td>";
                                    str += "<td width=\"142\">" + P + "</td>";
                                    str += "<td width=\"101\">" + PS + "</td>";
                                    str += "</tr>";
                                }
                                a++;
                            }
                        }
                    }
                }
            }

            return str;
        }
       /// <summary>
        /// 桌面视图
        /// </summary>
        /// <returns>桌面视图</returns>
        public ActionResult DeskWeekPlanView()
        {
            return View();
        }

        /// <summary>
        /// 绩效台账人员定义
        /// </summary>
        /// <returns></returns>
        public ActionResult Performanced() 
        {
            ViewData["tableJsonStr"] = GetTableJsonStr();
            string strTable = ViewData["tableJsonStr"].ToString();

            ViewData["strTable"] = GetEmpTable(strTable);

            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public string GetTableJsonStr(string returnValue = "")
        {
            string tableJsonStr = "";
            string entityNo = GlobalConstant.ENTITYNO;

            DataTable dt = monthPlanSummaryBLL.GetListByEntityNo(entityNo);

            tableJsonStr += "[";
            if (returnValue == "")
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        tableJsonStr += "{";
                        tableJsonStr += "\"FEmpName\":\"" + dr["FEmpName"].ToString() + "\",\"FEmpNo\":\"" + dr["FEmplNo"].ToString() + "\",\"FDeptName\":\"" + dr["deptName"].ToString() + "\",\"FDeptNo\":\"" + dr["FDeptNo"].ToString() + "\",\"Value\":\"" + dr["FEmplNo"].ToString() + "\"";
                        tableJsonStr += "},";
                    }
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            else
            {
                if (dt.Rows.Count > 0)
                {
                    List<Model.HREmpl> emplModelList1 = new List<Model.HREmpl>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        Model.HREmpl emplModel = new Model.HREmpl();
                        emplModel.FEmplNo = dr["FEmplNo"].ToString();
                        emplModel.FEmpName = dr["FEmpName"].ToString();
                        emplModel.FEntityNo = entityNo;
                        emplModel.FIsCheck = "N";
                        emplModelList1.Add(emplModel);
                    }
                    try
                    {
                        monthPlanSummaryBLL.UpdataEmpl(emplModelList1);
                    }
                    catch (Exception)
                    { }
                }

                List<SelectListItem> itemList = JsonConvert.DeserializeObject<List<SelectListItem>>(returnValue);
                List<Model.HREmpl> emplModelList = new List<Model.HREmpl>();

                foreach (SelectListItem selectItem in itemList)
                {
                    Model.HREmpl emplModel = new Model.HREmpl();
                    emplModel.FEmplNo = selectItem.Value;
                    emplModel.FEmpName = selectItem.Text;
                    emplModel.FEntityNo = entityNo;
                    emplModel.FIsCheck = "Y";
                    emplModelList.Add(emplModel);
                    tableJsonStr += emplBLL.GetEmpNameAndDept(entityNo, selectItem.Value) + ",";
                }
                //修考核人员参加网上考勤
                try
                {
                    monthPlanSummaryBLL.UpdataEmpl(emplModelList);
                }
                catch (Exception)
                { }

                if (itemList.Count > 0)
                {
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            tableJsonStr += "]";
            return tableJsonStr;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetEmpTable(string strTable)
        {
            string str = "";
            string deptNo = "";
            int td = 8;//一行显示的人员个数
            int ss = 0;//每个部门的总人数
            int tr = 0;
            List<Model.HREmpl> itemList = JsonConvert.DeserializeObject<List<Model.HREmpl>>(strTable);
            List<int> intList = new List<int>();

            for (int i = 0; i < itemList.Count; i++)
            {
                if (deptNo != itemList[i].FDeptNo)
                {
                    deptNo = itemList[i].FDeptNo;
                    if (ss != 0)
                    {
                        intList.Add(ss);
                    }
                    ss = 0;
                    ss++;
                    tr++;
                }
                else
                {
                    ss++;
                }
            }
            intList.Add(ss);

            List<int> trIntList = new List<int>();

            foreach (int p in intList)
            {
                int a = 0;
                a = p;
                if (a % td == 0)
                {
                    a = a / td;
                }
                else
                {
                    a = a / td + 1;
                }
                trIntList.Add(a);
            }


            deptNo = "";
            str += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"99%\" class=\"table\">";
            str += "<tr><td style=\"font-size:14px;font-weight:bold;text-align:center;width:100px;\">部门</td><td style=\"font-size:14px;font-weight:bold;text-align:center\" colspan=\"" + td + "\">人员</td></tr>";
            int j = 0;
            int sum = 0;
            int count = 0;
            foreach (Model.HREmpl model in itemList)
            {
                if (deptNo != model.FDeptNo)
                {
                    deptNo = model.FDeptNo;

                    if (j > 0)
                    {
                        if (sum < td)
                        {
                            for (int i = 0; i < td - sum; i++)
                            {
                                str += "<td>&nbsp;</td>";
                            }
                        }
                        str += "</tr>";
                    }

                    str += "<tr>";
                    str += "<td style=\"font-size:12px;font-weight:bold;text-align:center;\" rowspan=\"" + trIntList[j] + "\">" + model.FDeptName + "</td>";
                    str += "<td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";

                    j++;
                    sum = 0;
                    sum++;
                }
                else
                {
                    if (sum >= td)
                    {
                        str += "</tr><tr><td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";
                        //int abs = Math.Abs(sum - td - td) + 1;
                        //for (int i = 0; i < abs; i++)
                        //{
                        //    str += "<td class=\"tdClass\" width=\"50\">&nbsp;</td>";
                        //}
                        sum = 0;
                        count++;
                    }
                    else
                    {
                        str += "<td style=\"text-align:center;\" width=\"40\">" + model.FEmpName + "</td>";
                    }
                    sum++;
                }
            }
            //str += "</tr>";
            //int abs1 = Math.Abs(sum - td) + 2;
            //for (int i = 0; i < abs1; i++)
            //{
            //    str += "<td class=\"tdClass\" width=\"50\">&nbsp;</td>";
            //}
            //if (ss > 0)
            //{
            //    str += "</tr>";
            //}
            if (sum < td)
            {
                for (int i = 0; i < td - sum; i++)
                {
                    str += "<td>&nbsp;</td>";
                }
            }
            str += "</tr>";
            str += "</table>";
            return str;
        }

    }
}
